package com.cms.admin.action.common;

import com.cms.admin.action.BaseAction;
import com.cms.admin.model.*;
import com.cms.admin.service.common.GoodsListService;
import com.cms.admin.service.common.GoodsPriceListService;
import com.cms.admin.service.common.TemplateService;
import com.cms.admin.service.stuff.StuffService;
import com.cms.admin.util.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by YYY-Work on 2017/5/3.
 */
public class GoodsPriceListAction extends BaseAction implements ModelDriven<GoodsPriceListExt>{

    private GoodsPriceListService goodsPriceListService;
    private GoodsListService goodsListService;
    private TemplateService templateService;
    private StuffService stuffService;
    private GoodsPriceListExt search=new GoodsPriceListExt();
    private PageBean page;

    private Long stuffID;
    private StuffList stuffList;
    private List<GoodsListExt> goodsLists;
    private Long priceID;
    private GoodsPriceList goodsPriceList;

    public String ajaxQueryGoodsPriceList()
    {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());
        page = new PageBean(search.getCurrentPage(), search.getPageSize());

        List<GoodsPriceListExt> goodsPriceListExts=goodsPriceListService.queryGoodsPriceList(page,search);

        String res=null;

        try {
            res=getQueryResult(goodsPriceListExts,page);

            JSONObject o = new JSONObject();
            o.put("pageCount", page.getTotalPage());
            o.put("content", res);
            return ajaxJsonSuccessMessage(o);
        }catch (Exception e)
        {
            LOG.error("ajaxQueryHospitalList",e);
            return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
        }
    }

    public String ajaxQueryStuffGoodsPriceList()
    {
        initBathPath();
        stuffList=stuffService.queryStuffListById(stuffID);
        return "success";
    }

    /**
     * 返回新增价格页面
     * */
    public String requestAddStuffGoodsPriceList()
    {
        initBathPath();
        stuffList=stuffService.queryStuffListById(stuffID);
        goodsLists=goodsListService.queryAllGoodsList();
        return "success";
    }

    public String requestEditStuffGoodsPriceList()
    {
        stuffList=stuffService.queryStuffListById(stuffID);
        goodsLists=goodsListService.queryAllGoodsList();
        GoodsPriceList example=new GoodsPriceList();
        example.setPriceid(priceID);
        goodsPriceList=goodsPriceListService.queryStuffGoodsPriceListByPriceId(example);
        return "success";
    }

    public String requestGoodsPriceList()
    {
        return "success";
    }

    public String ajaxAddGoodsPriceList()
    {
        request=ServletActionContext.getRequest();
        initBathPath();
        String serialIdno=search.getSerialIdno();
        int goodsid=search.getGoodsid();
        Short columnid=search.getColumnid();

        GoodsPriceListExt example=new GoodsPriceListExt();
        example.setSerialIdno(serialIdno);
        example.setGoodsid(goodsid);
        example.setColumnid(columnid);

        List<GoodsPriceListExt> checkResults=goodsPriceListService.checkGoodsPriceListExit(example);
        if (checkResults==null||checkResults.size()==0)
        {
            search.setUpdateDate(new Date());
            search.setUpdateUserid(((UserList)request.getSession().getAttribute(Constants.USER_OBJECT)).getUserid());
            goodsPriceListService.addGoodsPriceList(search);

            return "success";
        }else
        {
            System.out.print("医生已经有此价格 不能新增");
        }

        return null;
    }

    public String ajaxEditGoodsPriceList()
    {
        request= ServletActionContext.getRequest();
        initBathPath();
        search.setUpdateDate(new Date());
        search.setUpdateUserid(((UserList)request.getSession().getAttribute(Constants.USER_OBJECT)).getUserid());
        int resule=goodsPriceListService.updateGoodsPriceList(search);
        System.out.print("更新结果："+resule);
        return "success";
    }

    private String getQueryResult(List<GoodsPriceListExt> goodsPriceListExts, PageBean page)
            throws Exception {
        initBathPath();
        HashMap map = new HashMap();
        map.put("goodsPriceList", goodsPriceListExts);
        map.put("page", page);
        map.put("basePath", getBasePath());

        String result = "";
        result = templateService.generateContentByFreeMarker(
                Constants.TEMPLATE_GOODSPRICELIST_QUERY_RESULT, map);
        return result;
    }

    @Override
    public GoodsPriceListExt getModel() {
        return search;
    }


    public GoodsPriceListService getGoodsPriceListService() {
        return goodsPriceListService;
    }

    public void setGoodsPriceListService(GoodsPriceListService goodsPriceListService) {
        this.goodsPriceListService = goodsPriceListService;
    }

    public TemplateService getTemplateService() {
        return templateService;
    }

    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }

    public GoodsPriceListExt getSearch() {
        return search;
    }

    public void setSearch(GoodsPriceListExt search) {
        this.search = search;
    }

    public PageBean getPage() {
        return page;
    }

    public void setPage(PageBean page) {
        this.page = page;
    }

    public Long getStuffID() {
        return stuffID;
    }

    public void setStuffID(Long stuffID) {
        this.stuffID = stuffID;
    }

    public StuffService getStuffService() {
        return stuffService;
    }

    public void setStuffService(StuffService stuffService) {
        this.stuffService = stuffService;
    }

    public StuffList getStuffList() {
        return stuffList;
    }

    public void setStuffList(StuffList stuffList) {
        this.stuffList = stuffList;
    }

    public GoodsListService getGoodsListService() {
        return goodsListService;
    }

    public void setGoodsListService(GoodsListService goodsListService) {
        this.goodsListService = goodsListService;
    }

    public List<GoodsListExt> getGoodsLists() {
        return goodsLists;
    }

    public void setGoodsLists(List<GoodsListExt> goodsLists) {
        this.goodsLists = goodsLists;
    }

    public Long getPriceID() {
        return priceID;
    }

    public void setPriceID(Long priceID) {
        this.priceID = priceID;
    }

    public GoodsPriceList getGoodsPriceList() {
        return goodsPriceList;
    }

    public void setGoodsPriceList(GoodsPriceList goodsPriceList) {
        this.goodsPriceList = goodsPriceList;
    }
}
